./data/ folder.
sf::read_sf() function to load the data. Sampling is straightforward: Apply the sf::st_sample to the loaded shapefile, but make sure to apply the sf::st_as_sf() function afterward to receive a full-fledged data table (with a geometry column only).
library(dplyr)
cologne <-
sf::read_sf("./data/cologne.shp")
cologne_50_points <-
cologne %>%
sf::st_sample(50) %>%
sf::st_as_sf()
immigrants_cologne.tif and inhabitants_cologne.tif files in the ./data/ folder.
library(raster)
immigrant_rates <-
terra::rast("./data/immigrants_cologne.tif") * 100 /
terra::rast("./data/inhabitants_cologne.tif")
immigrant_rates_at_point <-
terra::extract(immigrant_rates, terra::vect(cologne_50_points))
# There may be a lot of missing values.
buffer = 500. In that case, you should also set a descriptive statistics function, e.g., with the option fun = mean.
immigrant_rates_500m_buffer <-
terra::extract(
immigrant_rates,
terra::vect(cologne_50_points),
buffer = 500,
fun = mean
)